﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace PO_2011
{
    public class BitmapFastPixel
    {
        protected static Bitmap new_img;
        protected static Bitmap old_img; // pobierane za pomoca
        protected static int N;

        //potrzebne do getFastPixel()
        protected static System.Drawing.Imaging.BitmapData bmpData;
        protected static Rectangle rect;
        protected static byte[] rgbValues;
        protected static byte[] rgbValuesNewImage;

        protected static int[] getFastPixel(int x, int y)
        {
            int[] color = new int[3];

            // Lock the bitmap's bits.  
            if (rgbValues == null)
            {
                rect = new Rectangle(0, 0, old_img.Width, old_img.Height);
                bmpData = old_img.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, old_img.PixelFormat);

                IntPtr ptr = bmpData.Scan0;

                int bytes = Math.Abs(bmpData.Stride) * old_img.Height;
                rgbValues = new byte[bytes];
                rgbValuesNewImage = new byte[bytes];
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            }

            int pixel = x * 4 + bmpData.Stride * y;

            color[2] = rgbValues[pixel];
            color[1] = rgbValues[pixel + 1];
            color[0] = rgbValues[pixel + 2];

            return color;
        }

        protected static void setFastPixel(int x, int y, int R, int G, int B)
        {
            // Lock the bitmap's bits.  
            if (rgbValues == null)
            {
                rect = new Rectangle(0, 0, old_img.Width, old_img.Height);
                bmpData = old_img.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, old_img.PixelFormat);

                IntPtr ptr = bmpData.Scan0;

                int bytes = Math.Abs(bmpData.Stride) * old_img.Height;
                rgbValues = new byte[bytes];
                rgbValuesNewImage = new byte[bytes];

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            }

            int pixel = x * 4 + bmpData.Stride * y;

            rgbValuesNewImage[pixel] = (byte) B;
            rgbValuesNewImage[pixel + 1] = (byte) G;
            rgbValuesNewImage[pixel + 2] = (byte) R;
        }

        public static Bitmap getNewImage(bool close = false)
        {
            Bitmap bitmap = new Bitmap(old_img.Width, old_img.Height, PixelFormat.Format24bppRgb);

            BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, old_img.Width, old_img.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            Marshal.Copy(rgbValuesNewImage, 0, bd.Scan0, rgbValuesNewImage.Length);
            bitmap.UnlockBits(bd);

            if (close)
            {
                closeImage();
            }

            return bitmap;
        }

        public static void closeImage()
        {
            try
            {
                old_img.UnlockBits(bmpData);
                rgbValues = null;
            }
            catch (Exception exc)
            {
                MessageBox.Show("Błąd zamknięcia: " + exc.Message);
            }
        }


    }



}
